//------------------------------------------------------------------------
// Abstract: Implement of class Timer
//
// Authoring: XiaoHanxiong
//
// Time: 2014.03.02
//
// Static test:
//
// Version history:
//		Ver_1.0	: 2014.03.02	XiaoHanxiong
//------------------------------------------------------------------------
#include "stdafx.h"
#include "Timer.h"

namespace Library
{
	unsigned int Timer::m_timerNum = 0;

	Timer::Timer( bool start_flag /* = false */ )
	{
		InitTimer(start_flag);
		Timer::m_timerNum++;
	}

	Timer::~Timer()
	{

	}

	void Timer::InitTimer( bool start_flag )
	{
		if(start_flag)
		{
			m_startFlag = true;
			m_startTime = GetClock();
		}
		else
		{
			m_startFlag = false;
			m_startTime = 0;
		}
		m_elapsedTime = 0;
		m_bPause = false;
	}

	unsigned int Timer::TimerCount() /* static */
	{
		return Timer::m_timerNum;
	}

	clock_t Timer::GetClock()
	{
		return clock();
	}

	void Timer::Start()
	{
		if(!m_startFlag)
		{
			m_startFlag = true;
			m_startTime = GetClock();
		}
	}

	void Timer::Reset()
	{
		InitTimer(false);
	}

	void Timer::Pause()
	{
		if(m_startFlag && !m_bPause)
		{
			m_bPause = true;
			m_elapsedTime = GetClock() - m_startTime;
		}
	}

	void Timer::Resume()
	{
		if(m_startFlag && m_bPause)
		{
			m_bPause = false;
			m_startTime = GetClock();
		}
	}

	bool Timer::IsPaused()
	{
		return m_bPause;
	}

	bool Timer::IsStart()
	{
		return m_startFlag;
	}

	double Timer::Elapsed()
	{
		if(m_startFlag && !m_bPause)
			return double(m_elapsedTime + GetClock() - m_startTime);
		else
			return double(m_elapsedTime);
	}

	double Timer::ElapsedSec()
	{
		return double(Elapsed() / CLOCKS_PER_SEC);
	}

	double Timer::RemainSec()
	{
		return double((std::numeric_limits<std::clock_t>::max)())
			/ double(CLOCKS_PER_SEC);
	}

}// _Library_